﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BFlatCompile.Serializer
{
	public class Cell
	{
		public string Value { get; private set; }
		public bool IsBreak { get; private set; }
		public bool IsContinue { get; private set; }

		public Cell(string value)
		{
			this.Value = value;
			this.IsBreak = false;
			this.IsContinue = false;
		}

		public void DontUseThisFunctionUnlessYouKnowTheConsequences(string newValue)
		{
			this.Value = newValue;
		}

		public static Cell CreateBreak()
		{
			return new Cell(true);
		}

		public static Cell CreateContinue()
		{
			return new Cell(false);
		}

		// For break statements
		private Cell(bool isBreak)
		{
			this.Value = null;
			this.IsBreak = isBreak;
			this.IsContinue = !isBreak;
		}

		public void ResolveJump(Compiler c, int jumpOffset)
		{
			int sign = this.IsBreak ? 1 : -1;
			this.Value = ByteCodes.JUMP(c, sign * jumpOffset);
		}

		public override string ToString()
		{
			return this.Value == null ? "<NULL>" : this.Value;
		}
	}
}
